﻿using System;
using System.Collections.Generic;
using DataLogical;
using System.Data;
using Biz.Utility;

namespace Biz.UserManagement
{
    class RoleList
    {
        private int userID = Define.NEW;

        public int UserID
        {
            get { return userID; }
            set { userID = value; }
        }

        private List<Role> list = new List<Role>();
                
        public RoleList(int _userID)
        {
            List<UserRole> _userRolelist = UserRole.GetRoleList(_userID);
            foreach (UserRole ur in _userRolelist)
            {
                list.Add(new Role(ur.ROLE_ID));
            }
        }

        public RoleList()
        {
        }

        public bool HasRole(Role.RoleType _roleID)
        {
            foreach (Role r in list)
            {
                if (r.RoleID == _roleID)
                    return true;
            }
            return false;
        }

        public DataTable ListRole()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ROLE_NAME");
            foreach (Role r in list)
            {
                dt.Rows.Add(r.RoleName);
            }
            return dt;
        }

        public void RoleAdd(Role.RoleType _roleID)
        {
            foreach (Role r in list)
            {
                if (r.RoleID == _roleID)
                    return;
            }
            list.Add(new Role(int.Parse(_roleID.ToString())));
        }

        public void RoleDelete(Role.RoleType _roleID)
        {
            foreach (Role r in list)
            {
                if (r.RoleID == _roleID)
                {
                    list.Remove(r);
                }
            }
        }

        public void Save()
        {
            UserRole.DeleteUser(UserID);
            foreach (Role r in list)
            {
                UserRole ur = new UserRole();
                ur.USER_ID = UserID;
                ur.ROLE_ID = r.Id;
                ur.Insert(ur);
            }
        }
    }
}
